﻿<%@ WebHandler Language="C#" Class="ImageHandler" %>

using System;
using System.Web;
using System.IO;
public class ImageHandler : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        string photoName = context.Request.QueryString["ImageName"];
        string cachePath = "";
        if (context.Request.QueryString["ImagePath"] != null)
        {
            cachePath = context.Request.QueryString["ImagePath"] + photoName;
        }
        else
        {
            cachePath = Path.Combine(HttpRuntime.CodegenDir, photoName);
        }
        if (File.Exists(cachePath))
        {
            OutputCacheResponse(context, File.GetLastWriteTime(cachePath));
            context.Response.WriteFile(cachePath);
            return;
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    private static void OutputCacheResponse(HttpContext context, DateTime lastModified)
    {
        HttpCachePolicy cachePolicy = context.Response.Cache;
        cachePolicy.SetCacheability(HttpCacheability.Public);
        cachePolicy.VaryByParams["ImageName"] = true;
        cachePolicy.SetOmitVaryStar(true);
        cachePolicy.SetExpires(DateTime.Now + TimeSpan.FromDays(10));
        cachePolicy.SetValidUntilExpires(true);
        cachePolicy.SetLastModified(lastModified);
    }

}